home *** CD-ROM | disk | FTP | other *** search
- /* XDynArray.h
- *
- * This is my custom classes for manipulating a dynamic array
- * of objects. As this is pretty common, yet the 'dynarray' standard
- * C++ class is not declared for either VC++, Metrowerks C++, or
- * the GNU C++ library, I'm doing the minimum functionality I need here.
- */
-
- /* YAAF - Yet another application framework
- * Copyright (C) 1997 William Edward Woody and In Phase Consulting
- *
- * This library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Library
- * General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or any
- * later version.
- *
- * This library is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Library General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Library General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * To contact the author, either e-mail me at
- * woody@alumni.caltech.edu, or write to us at
- *
- * William Edward Woody
- * In Phase Consulting
- * 1545 Ard Eevin Avenue
- * Glendale, CA 91202
- */
-
- #ifndef __XDYNARRAY_H__
- #define __XDYNARRAY_H__
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=power
- #endif
- #if defined(__INTEL__)
- #pragma pack(push,2)
- #endif
- #endif
-
- /************************************************************************/
- /* */
- /* Class declarations */
- /* */
- /************************************************************************/
-
- /* XGDynArray
- *
- * This implements enough of a dynamic array template for me.
- */
-
- template <class T> class XGDynArray {
- public:
- XGDynArray()
- {
- fAlloc = 0;
- fLength = 0;
- fArray = NULL;
- }
- virtual ~XGDynArray()
- {
- if (fArray) delete[] fArray;
- }
-
- /*
- * Content manipulation
- */
-
- void Insert(long i,const T& t)
- {
- T *array;
- long j;
-
- /* Range check */
- if (i < 0) throw "Out of range";
- if (i > fLength) throw "Out of range";
-
- if (fArray == NULL) {
- /* Allocate new pointer fresh */
- fAlloc = 16;
- fLength = 1;
- fArray = new T[fAlloc];
- fArray[i] = t;
- } else {
- if (fLength >= fAlloc) {
- /* Grow allocation */
- array = new T[16+fAlloc];
- fAlloc += 16;
-
- /* Copy to new array */
- for (j = 0; j < i; j++) {
- array[j] = fArray[j];
- }
- array[i] = t;
- for (j = i; j < fLength; j++) {
- array[j+1] = fArray[j];
- }
- fLength++;
-
- /* Replace old array */
- delete[] fArray;
- fArray = array;
- } else {
- /* Move array around */
- for (j = fLength; j > i; i--) {
- fArray[j] = fArray[j-1];
- }
- fArray[i] = t;
- fLength++;
- }
- }
- }
-
- void Delete(long i)
- {
- T *array;
- long j;
-
- /* Range check */
- if (i < 0) throw "Out of range";
- if (i > fLength) throw "Out of range";
-
- /* Move objects over */
- for (j = i+1; j < fLength; j++) {
- fArray[j-1] = fArray[j];
- }
- fLength--;
-
- /* Shrink allocation (if needed) */
- j = fLength;
- if (j % 16) j += 16 - (j % 16);
- if (j == 0) j = 16;
- if (j != fAlloc) {
- /* Resize this thing */
- array = new T[j];
- if (array) {
- for (j = 0; j < fLength; j++) {
- array[j] = fArray[j];
- }
- delete[] fArray;
- fArray = array;
- }
- }
- }
-
- long Length() const
- {
- return fLength;
- }
-
- XGDynArray<T> & operator += (const T&t)
- {
- Insert(fLength,t);
- return *this;
- }
-
- T & operator [](long l)
- {
- return fArray[l];
- }
- private:
- long fLength;
- long fAlloc;
- T *fArray;
- };
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=reset
- #endif
- #if defined(__INTEL__)
- #pragma pack(pop)
- #endif
- #endif
-
- #endif
-